﻿'
' Will Strohl (will.strohl@gmail.com)
' http://www.willstrohl.com
'
'Copyright (c) 2011, Will Strohl
'All rights reserved.
'
'Redistribution and use in source and binary forms, with or without modification, are 
'permitted provided that the following conditions are met:
'
'Redistributions of source code must retain the above copyright notice, this list of 
'conditions and the following disclaimer.
'
'Redistributions in binary form must reproduce the above copyright notice, this list 
'of conditions and the following disclaimer in the documentation and/or other 
'materials provided with the distribution.
'
'Neither the name of Will Strohl nor the names of its contributors may be 
'used to endorse or promote products derived from this software without specific prior 
'written permission.
'
'THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
'EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
'OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
'SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
'INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
'TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
'BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
'CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
'ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
'DAMAGE.
'

Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.Security
Imports DotNetNuke.Services.Exceptions
Imports System.Web.UI.WebControls

Namespace WillStrohl.Modules.CodeCamp

    Partial Public MustInherit Class SpeakerRegistration
        Inherits WNSPortalModuleBase
        Implements IActionable
        
#Region " Private Members "

        Private Const c_MenuItem_EditSettings As String = "MenuItem.Settings"

        Private p_NoSettingsMessage As String = String.Empty
        Private p_SpeakerMessage As String = String.Empty
        Private p_CodeCampId As Integer = Null.NullInteger
        Private p_CodeCamp As CodeCampInfo = Nothing
        Private p_Speaker As SpeakerInfo = Nothing
        Private p_SpeakerCount As String = String.Empty
        Private p_SessionCount As String = String.Empty

#End Region

#Region " Private Properties "

        Protected ReadOnly Property EditSettingsUrl As String
            Get
                Return EditUrl(String.Empty, String.Empty, "Edit")
            End Get
        End Property

        Protected ReadOnly Property NoSettingsMessage As String
            Get
                Return String.Format(Me.GetLocalizedString("NoSettings.Text"), Me.EditSettingsUrl)
            End Get
        End Property

        Protected ReadOnly Property SpeakerMessage As String
            Get
                If String.IsNullOrEmpty(Me.p_SpeakerMessage) And Not Me.Settings("Message") Is Nothing Then
                    Me.p_SpeakerMessage = Me.Settings("Message").ToString
                End If

                Return Server.HtmlDecode(Me.p_SpeakerMessage)
            End Get
        End Property

        Private ReadOnly Property CodeCampId As Integer
            Get
                If Me.p_CodeCampId = Null.NullInteger And Not Me.Settings("CodeCampId") Is Nothing Then
                    Integer.TryParse(Me.Settings("CodeCampId").ToString, Me.p_CodeCampId)
                End If

                Return Me.p_CodeCampId
            End Get
        End Property

        Private ReadOnly Property CodeCamp As CodeCampInfo
            Get
                If Me.p_CodeCamp Is Nothing And Me.CodeCampId > 0 Then
                    Dim ctlCodeCamp As New CodeCampController
                    Me.p_CodeCamp = ctlCodeCamp.GetCodeCampById(Me.CodeCampId)
                End If

                Return Me.p_CodeCamp
            End Get
        End Property

        Private ReadOnly Property Speaker As SpeakerInfo
            Get
                If Me.p_Speaker Is Nothing Then
                    Dim ctlCodeCamp As New CodeCampController
                    Me.p_Speaker = ctlCodeCamp.GetSpeakerByUserId(Me.UserId)
                End If

                Return Me.p_Speaker
            End Get
        End Property

        Protected ReadOnly Property SpeakerCount As String
            Get
                If String.IsNullOrEmpty(Me.p_SpeakerCount) Then
                    Dim ctlModule As New CodeCampController
                    Me.p_SpeakerCount = ctlModule.CodeCampSpeakerCount(Me.CodeCampId).ToString
                End If

                Return Me.p_SpeakerCount
            End Get
        End Property

        Protected ReadOnly Property SessionCount As String
            Get
                If String.IsNullOrEmpty(Me.p_SessionCount) Then
                    Dim ctlModule As New CodeCampController
                    Me.p_SessionCount = ctlModule.CodeCampSessionCount(Me.CodeCampId).ToString
                End If

                Return Me.p_SessionCount
            End Get
        End Property

#End Region

#Region " Event Handlers "

        Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Try

                ' request that the DNN framework load the jQuery script into the markup
                DotNetNuke.Framework.jQuery.RequestRegistration()

                Me.RegisterClientScript("jQuery.UI", "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/jquery-ui.min.js")

                Me.RegisterCSS("jQuery.UI", "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/themes/ui-lightness/jquery-ui.css")

                Me.RegisterClientScript("DNNCodeCampCore", String.Concat(Me.ControlPath, "js/DNNCodeCamp.Core.js"))

                If Not Page.IsPostBack Then
                    Me.BindData()
                End If

            Catch exc As Exception ' Module failed to load
                ProcessModuleLoadException(Me, exc, Me.IsEditable)
            End Try
        End Sub

        Private Sub lnkRegister_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkRegister.Click
            Me.ToggleSpeakerForm()
        End Sub

        Private Sub lnkCancelSpeaker_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkCancelSpeaker.Click
            Me.ToggleSpeakerForm()
        End Sub

        Private Sub lnkSaveSpeaker_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkSaveSpeaker.Click

            If Page.IsValid Then

                Me.RegisterSpeaker()

            End If

        End Sub

        Private Sub lnkAddSession_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkAddSession.Click
            Me.ToggleSessionForm()
        End Sub

        Private Sub lnkSaveSession_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkSaveSession.Click
            If Page.IsValid Then
                Me.SaveSession()
            End If
        End Sub

        Private Sub rptSessions_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles rptSessions.ItemCommand

            Dim ctlCodeCamp As New CodeCampController

            Select Case (e.CommandName)

                Case "Edit"
                    ' show the edit session view
                    Dim oSession As SessionInfo = ctlCodeCamp.GetSession(Integer.Parse(e.CommandArgument.ToString, Globalization.NumberStyles.Integer))

                    Me.hidSessionId.Value = oSession.SessionId.ToString
                    Me.txtSessionTitle.Text = oSession.Title
                    Me.txtSessionDescription.Text = oSession.Description
                    For Each item As ListItem In Me.cboAudienceLevel.Items
                        item.Selected = (item.Value = oSession.AudienceLevel.ToString)
                    Next

                    Me.ToggleSessionForm()

                Case "Delete"
                    ' delete the current session
                    ctlCodeCamp.DeleteSession(Integer.Parse(e.CommandArgument.ToString, Globalization.NumberStyles.Integer))
                    Me.DataBind()
            End Select

        End Sub

        Private Sub lnkCancelSession_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkCancelSession.Click
            Me.ToggleSessionForm()
        End Sub

#End Region

#Region " Helper Methods "

        Private Sub BindData()

            Me.LocalizeModule()

            Me.pnlNoSettings.Visible = (Me.CodeCamp Is Nothing Or Me.CodeCampId < 1)

            If Not Me.CodeCamp Is Nothing Then

                Me.liAuthor.Visible = Me.CodeCamp.ShowAuthor
                Me.liMicrosoftMvp.Visible = Me.CodeCamp.ShowMicrosoftMVP
                Me.liShirtSize.Visible = Me.CodeCamp.ShowShirtSize

                If Me.cboShirtSize.Items.Count = 0 Then
                    Me.cboShirtSize.Items.Add(New ListItem(Me.GetLocalizedString("cboShirtSize.Size.Small")))
                    Me.cboShirtSize.Items.Add(New ListItem(Me.GetLocalizedString("cboShirtSize.Size.Medium")))
                    Me.cboShirtSize.Items.Add(New ListItem(Me.GetLocalizedString("cboShirtSize.Size.Large")))
                    Me.cboShirtSize.Items.Add(New ListItem(Me.GetLocalizedString("cboShirtSize.Size.ExtraLarge")))
                    Me.cboShirtSize.Items.Add(New ListItem(Me.GetLocalizedString("cboShirtSize.Size.ExtraExtraLarge")))
                    Me.cboShirtSize.Items.Add(New ListItem(Me.GetLocalizedString("cboShirtSize.Size.ExtraExtraExtraLarge")))
                    Me.cboShirtSize.Items.Insert(0, New ListItem("---"))
                End If

                If Me.cboAudienceLevel.Items.Count = 0 Then
                    Me.cboAudienceLevel.Items.Add(New ListItem(Me.GetLocalizedString("cboAudienceLevel.Items.Beginner"), "0"))
                    Me.cboAudienceLevel.Items.Add(New ListItem(Me.GetLocalizedString("cboAudienceLevel.Items.Intermediate"), "1"))
                    Me.cboAudienceLevel.Items.Add(New ListItem(Me.GetLocalizedString("cboAudienceLevel.Items.Advanced"), "2"))
                    Me.cboAudienceLevel.Items.Insert(0, New ListItem("---"))
                End If

            End If

            If Not Me.Settings("OpenDateTime") Is Nothing And Not Me.Settings("CloseDateTime") Is Nothing Then

                Dim dteBegin As DateTime = DateTime.Parse(Me.Settings("OpenDateTime").ToString)
                Dim dteEnd As DateTime = DateTime.Parse(Me.Settings("CloseDateTime").ToString)

                Me.pnlEarly.Visible = (dteBegin >= DateTime.Now)
                Me.pnlLate.Visible = (dteEnd <= DateTime.Now)

            End If

            If Me.UserId > 0 Then

                Me.pnlLogin.Visible = False
                Me.pnlRegistration.Visible = (Not Me.pnlNoSettings.Visible And Not Me.pnlEarly.Visible And Not Me.pnlLate.Visible)

                If Me.pnlRegistration.Visible Then

                    Dim ctlCodeCamp As New CodeCampController
                    Dim usrRegistration As New RegistrationInfo

                    usrRegistration = ctlCodeCamp.GetRegistrationByUser(Me.UserId)

                    If Not usrRegistration Is Nothing Then

                        Dim usrSpeaker As New SpeakerInfo
                        usrSpeaker = ctlCodeCamp.GetSpeakerByRegistrationId(usrRegistration.RegistrationId)

                        Me.pSpeakerButton.Visible = (usrSpeaker Is Nothing OrElse usrSpeaker.SpeakerId < 1)

                        Me.divMySessions.Visible = (Not Me.pSpeakerButton.Visible)

                        If Me.divMySessions.Visible Then
                            Me.LoadSessions()
                        End If

                    End If

                End If

            Else

                Me.pnlLogin.Visible = True

            End If

            Me.pnlStatus.Visible = (Me.IsEditable And Me.CodeCampId > 0)

        End Sub

        Private Sub LocalizeModule()

            Me.rfvSpeakerName.ErrorMessage = Me.GetLocalizedString("rfvSpeakerName.ErrorMessage")
            Me.rfvBio.ErrorMessage = Me.GetLocalizedString("rfvBio.ErrorMessage")

            Me.rfvSessionTitle.ErrorMessage = Me.GetLocalizedString("rfvSessionTitle.ErrorMessage")
            Me.rfvSessionDescription.ErrorMessage = Me.GetLocalizedString("rfvSessionDescription.ErrorMessage")
            Me.rfvAudienceLevel.ErrorMessage = Me.GetLocalizedString("rfvAudienceLevel.ErrorMessage")

        End Sub

        Private Sub ToggleSpeakerForm()

            Me.lnkRegister.Visible = (Not Me.lnkRegister.Visible)
            Me.divSpeakerInfo.Visible = (Not Me.divSpeakerInfo.Visible)

            If Me.divSpeakerInfo.Visible Then
                Me.PopulateSpeakerForm()
            End If

        End Sub

        Private Sub PopulateSpeakerForm()

            Dim ctlCodeCamp As New CodeCampController
            Dim usrRegistration As RegistrationInfo = ctlCodeCamp.GetRegistrationByUser(Me.UserId)

            If Not Me.Speaker Is Nothing Then

                Me.txtSpeakerName.Text = Me.Speaker.SpeakerName
                Me.txtCompanyName.Text = Me.Speaker.CompanyName
                Me.txtCompanyTitle.Text = Me.Speaker.CompanyTitle
                Me.txtCompanyUrl.Text = Me.Speaker.URL
                Me.chkMicrosoftMvp.Checked = Me.Speaker.MicrosoftMVP
                Me.chkAuthor.Checked = Me.Speaker.Author
                For Each item As ListItem In Me.cboShirtSize.Items
                    item.Selected = (item.Text = usrRegistration.ShirtSize)
                Next
                Me.txtBio.Text = Me.Speaker.Bio

            Else

                Me.txtSpeakerName.Text = Me.UserInfo.DisplayName
                Me.txtCompanyUrl.Text = Me.UserInfo.Profile.Website

            End If

        End Sub

        Private Sub ToggleSessionForm()

            Me.lnkAddSession.Visible = (Not Me.lnkAddSession.Visible)
            Me.divEditSession.Visible = (Not Me.divEditSession.Visible)
            Me.rptSessions.Visible = (Not Me.rptSessions.Visible)

            If Me.lnkAddSession.Visible Then

                ' reset the form fields
                Me.hidSessionId.Value = String.Empty
                Me.txtSessionTitle.Text = String.Empty
                Me.txtSessionDescription.Text = String.Empty
                Me.cboAudienceLevel.SelectedIndex = 0

                Me.LoadSessions()

            End If

        End Sub

        Private Sub RegisterSpeaker()

            Dim ctlCodeCamp As New CodeCampController
            Dim sec As New PortalSecurity
            Dim usrRegistration As New RegistrationInfo

            usrRegistration = ctlCodeCamp.GetRegistrationByUser(Me.UserId)

            ' add registration record, if necessary
            If usrRegistration Is Nothing Then
                Dim intRegistration As Integer
                If Me.CodeCamp.ShowShirtSize Then
                    intRegistration = ctlCodeCamp.AddRegistration(Me.CodeCampId, Me.UserId, Me.cboShirtSize.SelectedValue)
                Else
                    intRegistration = ctlCodeCamp.AddRegistration(Me.CodeCampId, Me.UserId, String.Empty)
                End If
                usrRegistration = ctlCodeCamp.GetRegistrationById(intRegistration)
            End If

            ' add new speaker record
            ctlCodeCamp.AddSpeaker(usrRegistration.RegistrationId, _
                sec.InputFilter(Me.txtSpeakerName.Text, PortalSecurity.FilterFlag.NoMarkup), _
                sec.InputFilter(Me.txtCompanyTitle.Text, PortalSecurity.FilterFlag.NoMarkup), _
                sec.InputFilter(Me.txtCompanyName.Text, PortalSecurity.FilterFlag.NoMarkup), _
                sec.InputFilter(Me.txtCompanyUrl.Text, PortalSecurity.FilterFlag.NoMarkup), _
                sec.InputFilter(Me.txtBio.Text, PortalSecurity.FilterFlag.NoScripting), _
                String.Empty, Me.chkMicrosoftMvp.Checked, Me.chkAuthor.Checked, False, Me.UserId)

            Me.ToggleSpeakerForm()
            Me.BindData()

        End Sub

        Private Sub SaveSession()

            Dim sec As New PortalSecurity
            Dim ctlCodeCamp As New CodeCampController

            If String.IsNullOrEmpty(Me.hidSessionId.Value) Then

                ctlCodeCamp.AddSession( _
                    Me.Speaker.SpeakerId, _
                    sec.InputFilter(Me.txtSessionTitle.Text, PortalSecurity.FilterFlag.NoMarkup), _
                    sec.InputFilter(Me.txtSessionDescription.Text, PortalSecurity.FilterFlag.NoMarkup), _
                    Integer.Parse(Me.cboAudienceLevel.SelectedValue, Globalization.NumberStyles.Integer), _
                    Me.UserId)

            Else

                ctlCodeCamp.UpdateSession( _
                    Integer.Parse(Me.hidSessionId.Value, Globalization.NumberStyles.Integer), _
                    Me.Speaker.SpeakerId, _
                    sec.InputFilter(Me.txtSessionTitle.Text, PortalSecurity.FilterFlag.NoMarkup), _
                    sec.InputFilter(Me.txtSessionDescription.Text, PortalSecurity.FilterFlag.NoMarkup), _
                    Integer.Parse(Me.cboAudienceLevel.SelectedValue, Globalization.NumberStyles.Integer), _
                    Me.UserId)

            End If

            Me.ToggleSessionForm()

        End Sub

        Private Sub LoadSessions()

            If Not Me.Speaker Is Nothing Then

                Dim ctlCodeCamp As New CodeCampController
                Dim collSessions As New SessionInfoCollection

                collSessions = ctlCodeCamp.GetSessionsBySpeakerId(Me.Speaker.SpeakerId)

                If Not collSessions Is Nothing AndAlso collSessions.Count > 0 Then

                    Me.pNoSessions.Visible = False

                    Me.rptSessions.Visible = True
                    Me.rptSessions.DataSource = collSessions
                    Me.rptSessions.DataBind()

                Else

                    Me.pNoSessions.Visible = True
                    Me.rptSessions.Visible = False

                End If

            End If

        End Sub

#End Region

#Region " IActionable Implementation "

        Public ReadOnly Property ModuleActions() As DotNetNuke.Entities.Modules.Actions.ModuleActionCollection Implements IActionable.ModuleActions
            Get
                Dim Actions As New Actions.ModuleActionCollection

                Actions.Add(GetNextActionID, Me.GetLocalizedString(c_MenuItem_EditSettings), _
                    String.Empty, String.Empty, String.Empty, _
                    Me.EditSettingsUrl, _
                    False, DotNetNuke.Security.SecurityAccessLevel.Edit, True, False)

                Return Actions
            End Get
        End Property

#End Region

    End Class

End Namespace